<template>
  <lay-layer v-model="visible11" :title="actionsMap[action]" :area="['500px']">
    <div style="padding: 20px">
      <lay-form :model="form" ref="layFormRef11" required label-width="110px">
        <lay-form-item label="类型名称" prop="name">
          <lay-input v-model="form.name" />
        </lay-form-item>
        <lay-form-item label="类型编码" prop="code">
          <lay-input v-model="form.code" />
        </lay-form-item>
        <lay-form-item label="卡类型" prop="card_type">
          <lay-radio v-model="form.card_type" name="card_type" :value="i" :label="e"
            v-for="(e, i) in cardTypesMap"></lay-radio>
        </lay-form-item>
        <lay-form-item label="卡号码(后四位)" prop="card_number">
          <lay-input v-model="form.card_number" />
        </lay-form-item>
      </lay-form>
      <div style="width: 100%; text-align: center">
        <lay-button size="sm" type="primary" @click="toSubmit">保存</lay-button>
        <lay-button size="sm" @click="toCancel">取消</lay-button>
      </div>
    </div>
  </lay-layer>
</template>

<script setup lang="ts">
import { actionsMap } from '@/dicts/actions';
import { computed, onMounted, ref } from 'vue';
import { cloneDeep } from 'lodash-es'
import { layer } from '@layui/layui-vue';
import { dayBooksApi } from '@/api/module/dayBooks';
import { cardTypesMap, tradingPlatformsMap } from '@/dicts/dayBooks';
import { tradingTypesApi } from '@/api/module/tradingTypes';

const action = ref('add'); // add | update
const visible11 = ref(false)
const form: any = ref({})
const tradingTypes = ref([])

const emits = defineEmits(['success'])

defineExpose({
  open
})

onMounted(() => {
  getTradingTypes()
})

function getTradingTypes() {
  tradingTypesApi.list({ pageSize: 1000 }).then(({ data }) => {
    // tradingTypes.value = data;
  })
}

function open(actionKey: string, row: any = {}) {
  action.value = actionKey;
  form.value = cloneDeep(row || {});
  visible11.value = true;
}

function toSubmit() {
  console.log(form.value, 'form');
  if (form.value.id) {
    tradingTypesApi.update(form.value).then(() => {
      layer.msg('保存成功！', { icon: 1, time: 1000 });
      visible11.value = false;
      emits('success')
    })


  } else {
    tradingTypesApi.add(form.value).then(() => {
      layer.msg('保存成功！', { icon: 1, time: 1000 });
      visible11.value = false;
      emits('success')
    })
  }
}

function toCancel() {
  visible11.value = false
}
</script>